package com.atguigu.tingshu.user.pattern.factory;

import com.atguigu.tingshu.user.pattern.DeliveryStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author: atguigu
 * @create: 2025-06-16 09:40
 */
@Slf4j
@Component
public class DeliveryStrategyFactory {

    /**
     * 按照类型注入，
     * Map中key就是实现类Bean的ID，Map中value就是实现类对象
     */
    @Autowired
    private Map<String, DeliveryStrategy> strategyMap;


    /**
     * 按照类型注入，将策略接口类型实现类对象注入到List集合中
     */
    //@Autowired
    //private List<DeliveryStrategy> strategyList;


    /**
     * 根据购买项目类型，返回策略实现类对象
     *
     * @param orderItemType 购买项目类型
     * @return
     */
    public DeliveryStrategy getStrategy(String orderItemType) {
        if (strategyMap.containsKey(orderItemType)) {
            return strategyMap.get(orderItemType);
        }
        log.error("没有找到{}对应的策略实现类对象", orderItemType);
        throw new RuntimeException("没有找到对应的策略实现类对象");
    }
}
